<template>
  <div class="myDialog">
    <!-- 弹窗组件 -->
    <el-dialog :visible.sync="dialogFormVisible" :title="title" :before-close="closeDialog" :close-on-click-modal="false" width="450px">
      <el-form
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :disabled="isDisabled"
        label-width="100px"
      >
        <el-form-item :label="$t('roleName')" prop="roleName">
          <el-input v-model.trim="form.roleName"/>
        </el-form-item>
        <el-form-item :label="$t('roleDesc')" prop="description">
          <el-input v-model="form.description" rows="3" type="textarea" maxlength="200" show-word-limit/>
        </el-form-item>
      </el-form>
      <div v-if="!isDisabled" slot="footer" class="dialog-footer">
        <el-button icon="el-icon-circle-close" @click="closeDialog">{{
          $t("cancel")
        }}</el-button>
        <el-button v-btnThrottle="doSave" type="primary" icon="el-icon-receiving">{{
          $t("save")
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import crudJob from '@/api/system/role'
import mySetting from '@/mixins/mySetting'
export default {
  name: 'MyDialog',
  mixins: [mySetting],
  props: {
    form: {
      type: Object,
      default: () => ({})
    },
    isDisabled: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      rules: {
        roleName: [
          { required: true, message: `${this.$t('pleaseInput')}${this.$t('roleName')}`, trigger: 'blur' }
        ]
      } // 表单校验规则
    }
  },
  methods: {
    // 新增数据
    doSave() {
      this.$refs['ruleForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.form.id) {
          // 存在id则是更新
          crudJob.edit({ id: this.form.id, ...this.form }).then(res => {
            this.doMerge(res)
          })
        } else {
          // 不存在id则是新增
          crudJob.add(this.form).then(res => {
            this.doMerge(res)
          })
        }
      })
    },
    // 打开弹窗
    openDialog() {
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate()
      })
    },
    // 关闭弹窗
    closeDialog() {
      this.$emit('closeDialog')
      this.dialogFormVisible = false
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
